package com.azt.novel.book.service.impl;


import com.azt.novel.book.dao.entity.BookChapter;
import com.azt.novel.book.dao.entity.BookInfo;
import com.azt.novel.book.dao.mapper.BookChapterMapper;
import com.azt.novel.book.dao.mapper.BookContentMapper;
import com.azt.novel.book.dao.mapper.BookInfoMapper;
import com.azt.novel.book.dto.resp.BookChapterAboutRespDto;
import com.azt.novel.book.dto.resp.BookChapterRespDto;
import com.azt.novel.book.manager.cache.BookChapterCacheManager;
import com.azt.novel.book.manager.cache.BookContentCacheManager;
import com.azt.novel.book.service.IBookChapterService;
import com.azt.novel.common.resp.RestResp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 小说章节 服务实现类
 * </p>
 *
 * @author azt
 * @since 2023-12-02
 */
@Service
public class BookChapterServiceImpl implements IBookChapterService {
    @Resource
    private BookInfoMapper bookInfoMapper;

    @Resource
    private BookChapterMapper bookChapterMapper;

    @Resource
    private BookContentMapper bookContentMapper;

    @Resource
    private BookChapterCacheManager bookChapterCacheManager;

    @Resource
    private BookContentCacheManager bookContentCacheManager;

    @Override
    public RestResp<BookChapterAboutRespDto> getLastChapterAbout(Long bookId) {
        //获取书信息
        BookInfo bookInfo = bookInfoMapper.selectById(bookId);
        //获取该书最后一章的信息
        BookChapterRespDto lastChapterDto = bookChapterCacheManager.getChapterById(bookInfo.getLastChapterId());
        //获取总章节数，内容概要30字
        LambdaQueryWrapper<BookChapter> bookChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookChapterLambdaQueryWrapper.eq(BookChapter::getBookId, bookId);
        //同书总章节数
        Long chapterCount = bookChapterMapper.selectCount(bookChapterLambdaQueryWrapper);
        //获取书本内容概要
        String bookContent = bookContentCacheManager.getBookContent(lastChapterDto.getId()).substring(0, 30);
        return RestResp.ok(BookChapterAboutRespDto.builder()
                .chapterInfo(lastChapterDto)
                .chapterTotal(chapterCount)
                .contentSummary(bookContent)
                .build());
    }

    @Override
    public RestResp<Long> getNextChapterId(Long chapterId) {
        //获取书本id、章节排序的下标
        BookChapterRespDto book = bookChapterCacheManager.getChapterById(chapterId);
        Long bookId = book.getBookId();
        Integer chapterNum = book.getChapterNum();

        LambdaQueryWrapper<BookChapter> bookChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookChapterLambdaQueryWrapper.eq(BookChapter::getBookId, bookId)
                .gt(BookChapter::getChapterNum,chapterNum)
                .orderByAsc(BookChapter::getChapterNum)
                .last("limit 1");
        return RestResp.ok(
                Optional.ofNullable(bookChapterMapper.selectOne(bookChapterLambdaQueryWrapper))
                        .map(BookChapter::getId)
                        .orElse(null));
    }

    @Override
    public RestResp<Long> getPreChapterId(Long chapterId) {
        //获取书本id、章节排序的下标
        BookChapterRespDto book = bookChapterCacheManager.getChapterById(chapterId);
        Long bookId = book.getBookId();
        Integer chapterNum = book.getChapterNum();

        LambdaQueryWrapper<BookChapter> bookChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookChapterLambdaQueryWrapper.eq(BookChapter::getBookId, bookId)
                .lt(BookChapter::getChapterNum,chapterNum)
                .orderByDesc(BookChapter::getChapterNum)
                .last("limit 1");
        return RestResp.ok(
                Optional.ofNullable(bookChapterMapper.selectOne(bookChapterLambdaQueryWrapper))
                        .map(BookChapter::getId)
                        .orElse(null));
    }

    @Override
    public RestResp<List<BookChapterRespDto>> listChapters(Long bookId) {
        QueryWrapper<BookChapter> bookChapterQueryWrapper = new QueryWrapper<>();
        bookChapterQueryWrapper.eq("book_id", bookId);
        List<BookChapter> bookChapters = bookChapterMapper.selectList(bookChapterQueryWrapper);
        List<BookChapterRespDto> bookChapterRespDtos = new ArrayList<>();
        for (BookChapter bookChapter : bookChapters) {
            BookChapterRespDto bookChapterRespDto = BookChapterRespDto.builder()
                            .chapterWordCount(bookChapter.getWordCount()).build();
            BeanUtils.copyProperties(bookChapter, bookChapterRespDto);
            bookChapterRespDtos.add(bookChapterRespDto);
        }
        return RestResp.ok(bookChapterRespDtos);
    }

}
